home *** CD-ROM | disk | FTP | other *** search
- Path: newshub.cts.com!filebank!jack.greene
- From: jack.greene@filebank.cts.com (JACK GREENE)
- Newsgroups: comp.lang.c
- Subject: Adding Arrays, anyone?
- Date: Thu, 25 Jan 96 22:03:00 -0800
- Organization: The File Bank BBS - Fallbrook, CA 619-728-7307 (data)
- Distribution: world
- Message-ID: <8B9852B.014F009DE3.uuout@filebank.cts.com>
- References: <8B97599.014F009D28.uuout@filebank.cts.com>
- Reply-To: jack.greene@filebank.cts.com (JACK GREENE)
- NNTP-Posting-Host: bbsmail.tfb.com
- X-Mailer: PCBoard/UUOUT Version 1.10
- X-News-Daemon: KSP-Mail v2.7d (Jan 10 1996)
-
- <<***** On 01/24/96, RACHEL K WARREN wrote to ALL: *****>>
- RK> From: warrenrk@falcon.jmu.edu (Rachel K. Warren)
- RK> Subject: Adding Arrays, anyone?
- RK> Date: 24 Jan 1996 23:53:26 GMT
- RK> Organization: James Madison University, Harrisonburg, VA
- RK> Message-ID: <4e6gpm$dfb@doc.jmu.edu>
- RK>
- RK> Could somebody give me an example of adding two arrays
- RK> together?
- RK> Say the first array has four mailboxes with "1" in it to, so
- RK> when you
- RK> print it out its,"1111" and the second array has four
- RK> mailboxes with the
- RK> number "2" in it, so that you print it out and its "2222", so
- RK> I could get
- RK> "3333"?
- RK>
- RK> I tried something like
- RK>
- RK> for(counter =0 ; counter != '\0'; counter++)
- RK> printf("%c",string1[counter] + string2[counter]
- RK>
- RK> and I got a bunch of garbage on the screen.
-
- :-) And I bet it didn't compile very well either :-)
-
- When adding strings together like you've shown above, C will
- concatenate as opposed to doing the math that you're asking
- it to do. Try converting to a numeric variable to
- add. I would try something like this:
-
- for (counter = 0; counter != '0'; counter++) {
- printf("%c", itoa(
- atoi( string1[counter] ) +
- atoi(string2[counter]) )
- };
-
- The functions:
- atoi() /* ascii to integer */
- itoa() /* integer to ascii */
-
- are in the STDLIB.H in borlands cpp4.51
-
- Of course somebody else might have it a little neater and I wouldn't
- mind seeing that either.
- The main idea is to convert the strings to integers. You could even
- change the printf() to use
- "%d" and not even bother with the itoa().
-
- Just a thought from an old BASIC and xBase hack learning C myself.
-
- -jack
-
- ---
- ■ VbReader 1.5 #NR ■ A wise person sees as much as ought, not as much can.
-